package com.cljh.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cljh.bean.Sale;
import com.cljh.bean.SaleTable;
import com.cljh.bean.User;
import com.cljh.controller.page.Code;
import com.cljh.controller.page.ProductPageBean;
import com.cljh.controller.page.Result;
import com.cljh.service.ProductService;
import com.cljh.service.ProductsService;
import com.cljh.service.SaleService;
import com.cljh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/echarts")
public class EchartsController {
    @Autowired
    UserService userService;
    @Autowired
    ProductsService productsService;
    @Autowired
    ProductService productService;
    @Autowired
    SaleService saleService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static final String FILES_KEY="FILES_FRONT_ALL";
    public static final String USER_KEY="USER_FRONT_ALL";


    @GetMapping("/quarter")
    public  Result getChart(){

        String userStr = stringRedisTemplate.opsForValue().get(USER_KEY);
        List<User> list =null;
        if (StrUtil.isBlank(userStr)){
            list = userService.userList();
            stringRedisTemplate.opsForValue().set(USER_KEY,JSONUtil.toJsonStr(list));
        }else {
            list= JSONUtil.toBean(userStr, new TypeReference<List<User>>() {
            }, true);//redis缓存取出数据
        }

        int q1=0;
        int q2=0;
        int q3=0;
        int q4=0;
        for (User u : list) {
            Date createTime = u.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter){
                case Q1: q1++;break;
                case Q2: q2++;break;
                case Q3: q3++;break;
                case Q4: q4++;break;
                default: break;
            }

        }

        List<Integer> arrayList = CollUtil.newArrayList(q1, q2, q3, q4);
        int code = arrayList != null ? Code.GET_OK : Code.GET_ERR;
        String msg=arrayList !=null ? "查到信息" : "未查到信息";
        return new Result(code,arrayList,msg);
    }

    @GetMapping("/pie")
    public Result getPie(){
        //1.获取缓存数据
        String jsonStr = stringRedisTemplate.opsForValue().get(FILES_KEY);
        List<ProductPageBean> pageBeans = null;
        //判断是否为空
        if (StrUtil.isBlank(jsonStr)){
            pageBeans = productService.ProductList();//数据库取出
            //缓存到redis
            stringRedisTemplate.opsForValue().set(FILES_KEY,JSONUtil.toJsonStr(pageBeans));
        }else {
            pageBeans= JSONUtil.toBean(jsonStr, new TypeReference<List<ProductPageBean>>() {
            }, true);//redis缓存取出数据

        }

//        System.out.println(pageBeans);
        Integer t1=0;
        Integer t2=0;
        Integer t3=0;

        for (ProductPageBean p : pageBeans) {
            String types = p.getTypes();

            if ("男款".equals(types)){
                t1++;
            }else if ("女款".equals(types)){
                t2++;
            }else if ("儿童款".equals(types)){
                t3++;
            }
        }
        List<Integer> arrayList = CollUtil.newArrayList(t1, t2, t3);
        int code = arrayList != null ? Code.GET_OK : Code.GET_ERR;
        String msg=arrayList !=null ? "查到信息" : "未查到信息";
        return new Result(code,arrayList,msg);
    }

    public static final String USER_NUM="USER_NUM";
    public static final String SALE_ALL="SALE_ALL";
    public static final String SALES_ALL="SALES_ALL";
    @GetMapping("/userNum")
    public Result userNum(){
        //计算用户人数
//        String userNum = stringRedisTemplate.opsForValue().get(USER_NUM);
//        Integer i = null;
//        if (StrUtil.isBlank(userNum)){
//            i = userService.countUserNum();
//            stringRedisTemplate.opsForValue().set(USER_NUM,JSONUtil.toJsonStr(i));
//        }else {
//            i= JSONUtil.toBean(userNum, new TypeReference<Integer>() {
//            }, true);//redis缓存取出数据
//        }

        int i = userService.countUserNum();

        //计算销量
        Double sum=0.0;
        String saleAllStr = stringRedisTemplate.opsForValue().get(SALE_ALL);
        List<SaleTable> saleTables=null;
        if (StrUtil.isBlank(saleAllStr)){
           saleTables = saleService.selectAll();
            stringRedisTemplate.opsForValue().set(SALE_ALL,JSONUtil.toJsonStr(saleTables));
        }else {
            saleTables= JSONUtil.toBean(saleAllStr, new TypeReference< List<SaleTable>>() {
            }, true);//redis缓存取出数据
        }

        for (SaleTable s :
                saleTables) {
            Double payCount = s.getPayCount();
            if (payCount!=null){
                sum+= payCount;
            }
        }

        //计算销售量
        Long sum1 = Long.valueOf(0);

        ////计算男女购买比例
        int man=0;
        int woman=0;

//        List<SaleTable> saleTables1 = saleService.selectAll();
        for (SaleTable s :saleTables
        ) {
            Long sumCount = s.getSumCount();
            String username = s.getUsername();
            User user = userService.selectByUsername(username);
            if (user!=null){
                String gender = user.getGender();
                if ("女".equals(gender)){
                    woman++;
                }else if ("男".equals(gender)){
                    man++;
                }
                if (sumCount!=null){
                    sum1+=sumCount;
                }
            }
        }

        int type1=0;
        int type2=0;
        int type3=0;
        String salesStr = stringRedisTemplate.opsForValue().get(SALES_ALL);
        List<Sale> sales=null;
        if (StrUtil.isBlank(salesStr)){
            sales = saleService.selectSale();
            stringRedisTemplate.opsForValue().set(SALES_ALL,JSONUtil.toJsonStr(sales));
        }else {
            sales= JSONUtil.toBean(salesStr, new TypeReference< List<Sale>>() {
            }, true);//redis缓存取出数据
        }

        for (Sale s :
                sales) {
            String types = s.getTypes();
            switch (types){
                case "男款": type1++;break;
                case "女款": type2++;break;
                case "儿童款": type3++;break;
            }

        }

        String visit = stringRedisTemplate.opsForValue().get("visit");

        HashMap<String, Object> map = new HashMap<>();
        map.put("userSum",i);
        map.put("sum",sum1);
        map.put("saleSum",sum);
        map.put("man",man);
        map.put("woman",woman);
        map.put("type1",type1);
        map.put("type2",type2);
        map.put("type3",type3);

        map.put("visit",visit);

        Integer code = map != null ? Code.GET_OK : Code.GET_ERR;
        String msg=map != null ? "查到信息" : "未查到信息";
        return new Result(code,map,msg);
    }



    public static final String SALES_MAN="SALES_MAN";
    public static final String SALES_WOMAN="SALES_WOMAN";
    public static final String SALES_CHILD="SALES_CHILD";
    @GetMapping("/types")
    public Result types(){
        int man1=0;
        int man2=0;
        int man3=0;
        int man4=0;
        int woman1=0;
        int woman2=0;
        int woman3=0;
        int woman4=0;
        int baby1=0;
        int baby2=0;
        int baby3=0;
        int baby4=0;
        String manStr = stringRedisTemplate.opsForValue().get(SALES_MAN);
        List<Sale> sales =null;
        if (StrUtil.isBlank(manStr)){
            sales = saleService.selectAllSales("男款");
            stringRedisTemplate.opsForValue().set(SALES_MAN,JSONUtil.toJsonStr(sales));
        }else {
            sales= JSONUtil.toBean(manStr, new TypeReference< List<Sale>>() {
            }, true);//redis缓存取出数据
        }


        for (Sale sa : sales) {
            Date createTime = sa.getSaleTable().getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter){
                case Q1: man1++;break;
                case Q2: man2++;break;
                case Q3: man3++;break;
                case Q4: man4++;break;
            }
        }
        String womanStr = stringRedisTemplate.opsForValue().get(SALES_WOMAN);
        List<Sale> sales1 =null;
        if (StrUtil.isBlank(womanStr)){
            sales1 = saleService.selectAllSales("女款");
            stringRedisTemplate.opsForValue().set(SALES_WOMAN,JSONUtil.toJsonStr(sales1));
        }else {
            sales1= JSONUtil.toBean(womanStr, new TypeReference< List<Sale>>() {
            }, true);//redis缓存取出数据
        }


        for (Sale sa : sales1) {
            Date createTime = sa.getSaleTable().getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter) {
                case Q1:
                    woman1++;
                    break;
                case Q2:
                    woman2++;
                    break;
                case Q3:
                    woman3++;
                    break;
                case Q4:
                    woman4++;
                    break;
            }
        }
//        List<Sale> sales2 = saleService.selectAllSales("儿童款");
        String childStr = stringRedisTemplate.opsForValue().get(SALES_CHILD);
        List<Sale> sales2 =null;
        if (StrUtil.isBlank(childStr)){
            sales2 = saleService.selectAllSales("儿童款");
            stringRedisTemplate.opsForValue().set(SALES_CHILD,JSONUtil.toJsonStr(sales2));
        }else {
            sales2= JSONUtil.toBean(childStr, new TypeReference< List<Sale>>() {
            }, true);//redis缓存取出数据
        }
        for (Sale sa : sales2) {
            Date createTime = sa.getSaleTable().getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter) {
                case Q1:
                    baby1++;
                    break;
                case Q2:
                    baby2++;
                    break;
                case Q3:
                    baby3++;
                    break;
                case Q4:
                    baby4++;
                    break;
            }
        }

        List<Integer> man = CollUtil.newArrayList(man1, man2, man3, man4);
        List<Integer> woman = CollUtil.newArrayList(woman1, woman2, woman3, woman4);
        List<Integer> baby = CollUtil.newArrayList(baby1, baby2, baby3, baby4);
        HashMap<String, Object> map = new HashMap<>();

        map.put("man",man);
        map.put("woman",woman);
        map.put("baby",baby);

        Integer code = map != null ? Code.GET_OK : Code.GET_ERR;
        String msg=map != null ? "查到信息" : "未查到信息";
        return new Result(code,map,msg);
    }


}
